import defaultAxios from 'axios';
import { message } from 'antd';
import nProgress from 'nprogress';
import 'nprogress/nprogress.css';
const axios = defaultAxios.create({
  baseURL: 'http://fangadmin.shbwyz.com',
});
const whiteList = [
  {
    method: 'get',
    url: ' /getChatLog',
  },
];
defaultAxios.interceptors.request.use((config: any) => {
  nProgress.start();
  return config;
});
defaultAxios.interceptors.response.use(
  (data: any) => {
    let flag = whiteList.some((item: any) => {
      return item.method == data.config.method && item.url === data.config.url;
    });
    nProgress.done();
    if (flag) {
      message.success('请求成功');
    }
    return data;
  },
  (error: any) => {
    let code = error.response.status;
    switch (code) {
      case '301':
        console.log('请求的网页已永久移动到新位置');
        break;
      case '302':
        console.log('请求的网页临时移动');
        break;
      case '401':
        console.log('身份验证出现问题,token过期');
        break;
      case '403':
        console.log('服务器拒绝请求');
        break;
      case '404':
        console.log('页面未找到');
        break;
      case '500':
        console.log('服务器内部错误');
        break;
      case '502':
        console.log('网关错误');
        break;
      default:
        break;
    }
    message.err(error.message);
    return Promise.reject(error);
  },
);
axios.interceptors.request.use((config: any) => {
  config.headers.Authorization = sessionStorage.getItem('token')
    ? sessionStorage.getItem('token')
    : '';
  return config;
});
axios.interceptors.response.use((data: any) => {
  return data;
});
export { axios, defaultAxios };
